package com.xflib.shardingsphere.controller;

import com.xflib.shardingsphere.entity.Order;
import com.xflib.shardingsphere.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单 API
 *
 * @author Jiahai
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 新增
     *
     * @return
     */
    @PostMapping
    public Order save(@RequestBody Order order) {
        Order o = orderService.save(order);
        return o;
    }

    /**
     * 删除
     *
     * @param orderId 订单ID
     */
    @DeleteMapping("/{orderId}")
    public void delete(@PathVariable Long orderId) {
        orderService.delete(orderId);
    }

    /**
     * 修改
     *
     * @param order 订单
     */
    @PutMapping
    public Order update(@RequestBody Order order){
        return orderService.update(order);
    }

    /**
     * 根据主键ID查询
     *
     * @param orderId 订单ID
     * @return
     */
    @GetMapping("/{orderId}")
    public Order findById(@PathVariable Long orderId) {
        return orderService.findById(orderId);
    }

    /**
     * 列表查询（根据主键ID顺序）
     *
     * @return
     */
    @GetMapping("/list")
    public List<Order> list() {
        return orderService.list();
    }

    /**
     * 分页查询（根据主键ID逆序）
     *
     * @param page 当前页
     * @param size 每页条数
     * @return Page<Order> 分页对象
     */
    @GetMapping("/page/{page}/{size}")
    public Page<Order> page(@PathVariable int page, @PathVariable int size) {
        return orderService.page(page, size);
    }

    @GetMapping("/findByOrderName/{orderName}")
    public List<Order> findByOrderName(@PathVariable("orderName") String orderName){
        return orderService.findByOrderName(orderName);
    }
}
